To make MongoDB database manipulation easy, we can use the Mongoose NPM package to make working with MongoDB databases easier.
In this article, we’ll look at how to use Mongoose to manipulate our MongoDB database.
Modify Nested Documents
We can modify nested documents by accessing the path and then set the value for it.
For example, we can write:
async function run() {
const mongoose = require('mongoose');
const connection = mongoose.createConnection('mongodb://localhost:27017/test');
const childSchema = new mongoose.Schema({ name: 'string' });
const parentSchema = new mongoose.Schema({
children: [childSchema],
child: childSchema
});
const Child = await connection.model('Child', childSchema);
const Parent = await connection.model('Parent', parentSchema);
const parent = new Parent({ children: [{ name: 'Matt' }, { name: 'Sarah' }] })
await parent.save();
parent.children[0].name = 'Mary';
await parent.save();
console.log(parent);
}
run();
We get the children
subarray’s first entry’s name
property and set it to 'Mary'
.
Then we call save
on the parent
to save everything.
We can also call the set
method to do the same thing.
For example, we can write:
async function run() {
const mongoose = require('mongoose');
const connection = mongoose.createConnection('mongodb://localhost:27017/test');
const childSchema = new mongoose.Schema({ name: 'string' });
const parentSchema = new mongoose.Schema({
children: [childSchema],
child: childSchema
});
const Child = await connection.model('Child', childSchema);
const Parent = await connection.model('Parent', parentSchema);
const parent = new Parent({ children: [{ name: 'Matt' }, { name: 'Sarah' }] })
await parent.save();
parent.set({ children: [{ name: 'Mary' }] });
await parent.save();
console.log(parent);
}
run();
We replaced the children
array with the given entry.
Then we save the parent and child entries with the save
method.
Adding Subdocuments to Arrays
We can add subdocuments to arrays.
For example, we can write:
async function run() {
const mongoose = require('mongoose');
const connection = mongoose.createConnection('mongodb://localhost:27017/test');
const childSchema = new mongoose.Schema({ name: 'string' });
const parentSchema = new mongoose.Schema({
children: [childSchema],
child: childSchema
});
const Child = await connection.model('Child', childSchema);
const Parent = await connection.model('Parent', parentSchema);
const parent = new Parent({ children: [{ name: 'Matt' }, { name: 'Sarah' }] })
parent.children.push({ name: 'Jane' });
await parent.save();
console.log(parent);
}
run();
We call push
on the children
array to append an entry to the children
subarray.
Removing Subdocuments
To remove subdocuments, we can call the remove
method.
For example, we can write:
async function run() {
const mongoose = require('mongoose');
const connection = mongoose.createConnection('mongodb://localhost:27017/test');
const childSchema = new mongoose.Schema({ name: 'string' });
const parentSchema = new mongoose.Schema({
children: [childSchema],
child: childSchema
});
const Child = await connection.model('Child', childSchema);
const Parent = await connection.model('Parent', parentSchema);
const parent = new Parent({ children: [{ name: 'Matt' }, { name: 'Sarah' }] })
await parent.children[0].remove();
await parent.save();
console.log(parent);
}
run();
to remove a document from the children
array.
To remove an object, we can call the remove
method on the object:
async function run() {
const mongoose = require('mongoose');
const connection = mongoose.createConnection('mongodb://localhost:27017/test');
const childSchema = new mongoose.Schema({ name: 'string' });
const parentSchema = new mongoose.Schema({
children: [childSchema],
child: childSchema
});
const Child = await connection.model('Child', childSchema);
const Parent = await connection.model('Parent', parentSchema);
const parent = new Parent({ child: { name: 'Matt' } })
await parent.child.remove();
await parent.save();
console.log(parent);
}
run();
Conclusion
We can work with nested MongoDB documents easily with Mongoose.